<template>
    <div class="course-plan">
      <div class="plan-title" v-if="planTableShow" @click="gotoTable()">意志力鼓励计划进度 <img src="https://mobuz-mp.cdn.bcebos.com/matter/willPlanTableClick.png"></div>
      <div class="plan-list" :style="{'marginTop': planTableShow ? '0' : '30rpx'}">
        <div v-for="(item,index) in willPlan.plan" :key="index" @click="gotoCourseCircumstance(item)">
          <planCard :plan="item" :starShow="planTableShow"></planCard>
        </div>
      </div>
    </div>
</template>

<script>
import {obtainCourseWillPlan,statAwardsCase} from '@/config/api'
import {dateRegExp, commentRegExp} from '@/config/mUtils'
import {showMessager} from '@/config/wUtils'
import {remidnUserSocketBadge} from '@/config/vuexUtils'
import planCard from '@/components/planCard'
import {NEWMODULEBATCHID} from '@/config/env'
export default {
  components:{
    planCard
  },
  data () {
    return {
      // 课程卡片数据
      willPlan: {},
      // 课程开始时间
      courseStartTime: 0,
      // 一天的毫秒数
      day_ms:  60 * 60 * 24 * 1000,
      // 进入新界面，防止他重复点击
      astrictClickFlag: false,
      // 对老用户不开放课程计划表
      planTableShow: false,
      // 旧用户不显示星评
      starShow: false
    }
  },
  mounted () {
    //  this.courseWillPlan();
    // remidnUserSocketBadge('20181107015021')
    this.userInfo = wx.getStorageSync('userInfo');
    this.batch_id = wx.getStorageSync('batch_id');
    this.student_id = wx.getStorageSync('student_id');
    let status  = wx.getStorageSync('plan_state')
    if (status == 1) {
      this.courseWillPlan()
      wx.setStorageSync('plan_state',0)
      wx.setStorageSync('plan_table_state',1)
    } else if (!this.willPlan.plan) {
        this.courseWillPlan()
        wx.setStorageSync('plan_state',0)
        wx.setStorageSync('plan_table_state',1)
    }
    if (this.batch_id <= NEWMODULEBATCHID) {
      this.planTableShow = false;
    } else {
      this.planTableShow = true;
    }
  },
  methods: {
    async courseWillPlan () {
      wx.showLoading({title: '加载中'})
      await obtainCourseWillPlan(this.batch_id,this.userInfo['unionId'])
          .then(res => {
            if (res.status === 200) {
                wx.pageScrollTo({
                  scrollTop: 0
                });
                this.willPlan = res.data[0];
                this.courseStartTime = res.data[0].startTime - 0
                console.log(this.courseStartTime,'课程计划')
              }
          })

      wx.hideLoading()
      wx.stopPullDownRefresh()
    },
    // 跳转课程计划表
    gotoTable () {
      wx.navigateTo({
          url: '../willPlanTable/main'
      })
    },
    // 跳转详情界面
    async gotoCourseCircumstance (item) {
      // 今天零点时间
      let currentDay = new Date(new Date(new Date().toLocaleDateString()).getTime()).getTime();
      // 点击的日历时间
      let itemDate = this.courseStartTime + ((item['section_day'] - 1 )*this.day_ms);;
      if (item.section_id) {
          if (item.status == 0) {
              showMessager("尚未解锁");
              return false;
          }
          // 预防重复点击
          if (!this.astrictClickFlag) {
              this.astrictClickFlag = true;
              setTimeout(async () => {
                  if (this.courseStartTime >= 1542643200000) {
                      await statAwardsCase(this.student_id,this.batch_id)
                          .then(res => {
                              this.astrictClickFlag = false;
                              if (res.status === 200) {
                                //   wx.navigateTo({
                                //       url: '../courseCircumstance/main?section_id='+item.section_id+'&batch_id='+this.batch_id
                                //   })
                                this.gotoPage(item)
                              } else if (res.status === 202) {
                                  // 提醒只有一次
                                  if (!this.remindFlag) {
                                      wx.showModal({
                                          content: '课程结束后，5天内拥有课程回看权限',
                                          showCancel: false,
                                          success: ()=> {
                                            //   wx.navigateTo({
                                            //       url: '../courseCircumstance/main?section_id='+item.section_id+'&batch_id='+this.batch_id
                                            //   })
                                            this.gotoPage(item)
                                          }
                                      })
                                      this.remindFlag = true;
                                  } else {
                                    //   wx.navigateTo({
                                    //       url: '../courseCircumstance/main?section_id='+item.section_id+'&batch_id='+this.batch_id
                                    //   })
                                    this.gotoPage(item)
                                  }

                              } else {
                                  showMessager(res.msg);
                              }

                          })
                          .actch( err => {
                              showMessager("网络拥堵，请稍后再试");
                          })
                  } else {
                        this.astrictClickFlag = false;
                    //   wx.navigateTo({
                    //       url: '../courseCircumstance/main?section_id='+item.section_id+'&batch_id='+this.batch_id
                    //   })
                        this.gotoPage(item)
                  }

              }, 50);
          }
      }

    },
    // 跳转页面
    gotoPage (item) {
        if (this.batch_id <= NEWMODULEBATCHID) {
            wx.navigateTo({
                url: '../courseParticulars/main?section_id='+item.section_id+'&batch_id='+this.batch_id
            })
        } else {
            wx.navigateTo({
                url: '../courseCircumstance/main?section_id='+item.section_id+'&batch_id='+this.batch_id
            })
        }
    }
  },
  // 下拉刷新
  onPullDownRefresh () {
    this.courseWillPlan()
  },
  // 上拉加载
  onReachBottom () {
  },
  // 切换界面
  onHide () {
  },
  // 滚动界面
  onPageScroll (res) {
  },
  // 页面加载
  onLoad () {
  },
  // 页面显示
  onShow () {
    // let pages = getCurrentPages();
    // let Page = pages[pages.length - 1];//当前页
    // let prevPage = pages[pages.length - 2];  //上一个页面
    // console.log('当前页：',Page)
    // console.log('上一页页：',pages)

    this.userInfo = wx.getStorageSync('userInfo');
    this.batch_id = wx.getStorageSync('batch_id');
    this.student_id = wx.getStorageSync('student_id');
    let status  = wx.getStorageSync('plan_state')
    if (status == 1) {
      this.courseWillPlan()
      wx.setStorageSync('plan_state',0)
      wx.setStorageSync('plan_table_state',1)
    } else if (!this.willPlan.plan) {
        this.courseWillPlan()
        wx.setStorageSync('plan_state',0)
        wx.setStorageSync('plan_table_state',1)
    }
    if (this.batch_id <= NEWMODULEBATCHID) {
      this.planTableShow = false;
    } else {
      this.planTableShow = true;
    }
  }
}
</script>
<style>
page {
  height: 100%;
  background: #fafafa;
}
</style>
<style lang="less" scoped>
.course-plan {
  padding: 0 32rpx;
  .plan-title {
    padding: 31rpx 0 25rpx;
    line-height: 1;
    font-family: PingFangSC-Medium;
    font-size: 36epx;
    color: #333333;
    img {
        display: inline-block;
        vertical-align: middle;
        width: 24rpx;
        height: 24rpx;
    }
  }
  .plan-list {
    > div {
      margin-bottom: 30rpx;
    }
  }
}
</style>
